package gui.windows;

import gui.GUIHandler;
import gui.GUIManager;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;

import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;

import config.LauncherHelper;


import login.LoginHandler;
import app.AppGlobals;

@SuppressWarnings("serial")
public class LoginTab extends JPanel {
	private JPasswordField textField_password;
	private JTextField textField_userName;
	final JCheckBox chckbxLogMeIn = new JCheckBox("Auto-Login");
	
	public LoginTab(){
		this.setLayout(null);
		this.setBounds(32, 102, 976, 440);
		
		JEditorPane panel = new JEditorPane();
		panel.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panel.setEditable(false);
		panel.setBackground(Color.WHITE);
		panel.setBounds(10, 11, 857, 385);

		HTMLEditorKit kit = new HTMLEditorKit();
		panel.setEditorKit(kit);
		StyleSheet styleSheet = kit.getStyleSheet();
		
		styleSheet.addRule("h1 {font-size:30px}");
		styleSheet.addRule("body{ font-family: 'Quattrocento Sans', sans-serif; font-size:23px}");
		styleSheet.addRule(".entry {font-size: 5em;line-height: 120%;}");
		styleSheet.addRule("h1, h2, h3 {margin:0;};");
		styleSheet.addRule(".post-metadata {font-size:5em;}"); 
		styleSheet.addRule("article {margin-top:25px;margin-left:50px;}");

		kit.setStyleSheet(styleSheet);
		Document doc = kit.createDefaultDocument();
		panel.setDocument(doc);
		
		try {
			panel.setPage("http://devpro.org/category/patch-note/");
			panel.setContentType("text/html");
		} catch (IOException e1) {
			panel.setText("<html>Could not load</html>");
			e1.printStackTrace();
		}
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 857, 385);
		add(scrollPane);
		
		scrollPane.setViewportView(panel);
		
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(10, 407, 71, 14);
		add(lblUsername);		

		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(301, 407, 84, 14);
		add(lblPassword);
		
		textField_password = new JPasswordField();
		textField_password.setBounds(386, 407, 200, 17);
		add(textField_password);
		textField_password.addKeyListener(new KeyAdapter(){
			public void keyReleased(KeyEvent e ){
				if(e.getKeyCode() == KeyEvent.VK_ENTER ){
					onLogin();
				}}});
		
		textField_userName = new JTextField();
		textField_userName.setBounds(91, 407, 200, 20);
		add(textField_userName);
		textField_userName.setColumns(10);
				GUIManager.textField_userName = textField_userName;
		if(!AppGlobals.launcherConfig.defaultUsername.equals("")){
			textField_userName.setText(AppGlobals.launcherConfig.defaultUsername);
		}
		
		JButton btnLogin = new JButton("Login");
		btnLogin.setBounds(877, 373, 89, 23);
		add(btnLogin);
		GUIManager.btnLogin = btnLogin;
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				onLogin();
			}
		});
		
		JButton btnRegister = new JButton("Register");
		btnRegister.setBounds(877, 339, 89, 23);
		add(btnRegister);
		GUIManager.btnRegister = btnRegister;
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				GUIManager.btnRegister.setEnabled(false);
				GUIHandler.launchRegisterWindow();
			}
		});
		
		JButton btnOffline = new JButton("Offline");
		btnOffline.setBounds(877, 305, 89, 23);
		add(btnOffline);
		btnOffline.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				LauncherHelper.runGame();
			}
		});
		
		JButton btnSite = new JButton("Site");
		btnSite.setBounds(877, 271, 89, 23);
		add(btnSite);
		btnSite.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				LauncherHelper.openPageInDefaultBrowser("http://devpro.org");
			}
		});
		
		chckbxLogMeIn.setBounds(877, 405, 98, 23);
		add(chckbxLogMeIn);
		
		JComboBox<String> comboBox = new JComboBox<String>();
		comboBox.setBounds(701, 407, 163, 20);
		comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"English", "Deutsch"}));
		add(comboBox);
		
		JLabel lblLanguage = new JLabel("Language");
		lblLanguage.setBounds(624, 407, 84, 14);
		add(lblLanguage);
		
		GUIManager.mainHomeTab = this;
		
		JButton aboutBtn = new JButton("About");
		aboutBtn.setBounds(877, 237, 89, 23);
		add(aboutBtn);
		aboutBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				LauncherHelper.openPageInDefaultBrowser("http://devpro.org/staff/");
			}
		});
		

	}
	
	private void onLogin(){
		GUIManager.btnLogin.setEnabled(false);
		AppGlobals.localUserName = textField_userName.getText();
		AppGlobals.launcherConfig.defaultUsername = textField_userName.getText();
		LoginHandler.sendLogin(textField_userName.getText(), new String(textField_password.getPassword()));
		if(chckbxLogMeIn.isSelected()){
			AppGlobals.launcherConfig.password =  new String(textField_password.getPassword());
			AppGlobals.launcherConfig.autoLogin = true;
		}else{
			AppGlobals.launcherConfig.password =  "";
			AppGlobals.launcherConfig.autoLogin = false;
		}
	}
}
